package com.studentManagement.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;


/**
 * @author free
 */
@WebFilter(filterName = "Utf_8Filter")
public class Utf_8Filter implements Filter {
    private String encoding;
    private HashMap<String, String> params = new HashMap<String, String>();

    /**
      * 项目结束时就已经进行销毁
      * @author：刘富国
      * @创建时间：2018/5/22 22:02
      * @param：
      * @return:
      */
    @Override
    public void destroy() {
        params = null;
        encoding = null;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        response.setContentType("text/html;charset="+encoding);
        chain.doFilter(request, response);
    }


    /**
     * 项目启动时就已经进行读取
     * @author：刘富国
     * @创建时间：2018/5/22 22:01
     * @param：[config]
     * @return: void
     */
    @Override
    public void init(FilterConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
        for (Enumeration<?> e = config.getInitParameterNames(); e.hasMoreElements(); ) {
            String name = (String) e.nextElement();
            String value = config.getInitParameter(name);
            params.put(name, value);
        }
    }

}
